<template>
<a-radio-group v-if="tagType == 'radio'" @change="handleInput" :value="getValueSting" :disabled="disabled">
    <a-radio v-for="(item, key) in dictOptions" :key="key" :value="item.value">{{ item.text }}</a-radio>
</a-radio-group>

<a-radio-group v-else-if="tagType == 'radioButton'" buttonStyle="solid" @change="handleInput" :value="getValueSting" :disabled="disabled">
    <a-radio-button v-for="(item, key) in dictOptions" :key="key" :value="item.value">{{ item.text }}</a-radio-button>
</a-radio-group>

<a-select v-else-if="tagType == 'select'" :getPopupContainer="(target) => target.parentNode" :value="getValueSting" show-search placeholder="搜索查找" :options="option" @change="handleInput" />
</template>

<script>
import {
    ajaxGetDictItems,
    getDictItemsFromCache
} from '@/api/api'

export default {
    name: 'JDictSelectSearch',
    props: {
        dictCode: String,
        placeholder: String,
        triggerChange: Boolean,
        disabled: Boolean,
        value: [String, Number],
        type: String,
    },
    data() {
        return {
            dictOptions: [],
            tagType: '',
            filterOption: (input, option) => {
                return option.value.toLowerCase().indexOf(input.toLowerCase()) >= 0
            },
            option: [],
        }
    },
    watch: {
        dictCode: {
            immediate: true,
            handler() {
                this.initDictData()
            },
        },
    },
    created() {
        // console.log(this.dictCode);
        if (!this.type || this.type === 'list') {
            this.tagType = 'select'
        } else {
            this.tagType = this.type
        }
        //获取字典数据
        // this.initDictData();
    },
    computed: {
        getValueSting() {
            return this.value != null ? this.value.toString() : null
        },
    },
    methods: {
        initDictData() {
            //优先从缓存中读取字典配置
            if (getDictItemsFromCache(this.dictCode)) {
                this.dictOptions = getDictItemsFromCache(this.dictCode)
                return
            }

            //根据字典Code, 初始化字典数组
            ajaxGetDictItems(this.dictCode, null).then((res) => {
                if (res.success) {
                    this.dictOptions = res.result
                    this.option = []
                    for (var elem of this.dictOptions) {
                        let obj = {}
                        obj.value = elem.text || elem.label
                        obj.label = elem.text || elem.label
                        this.option.push(obj)
                    }
                }
            })
        },
        getNameId(num) {
            let array = this.dictOptions.filter((elem) => {
                if ((elem.text || elem.label) == num) {
                    return elem
                }
            })
            return array[0].value
        },
        handleInput(e) {
            let val
            if (this.tagType == 'radio') {
                val = e.target.value
            } else {
                val = e
            }
            if (this.triggerChange) {
                this.$emit('change', val)
                this.$emit('sendNameId', this.getNameId(val))
            } else {
                this.$emit('input', val)
            }
        },
        setCurrentDictOptions(dictOptions) {
            this.dictOptions = dictOptions
        },
        getCurrentDictOptions() {
            return this.dictOptions
        },
    },
}
</script>

<style scoped>
</style>
